<?php

namespace app\Modules\Api\controllers;

use Yii;
use app\Modules\Api\models\User;
use app\Modules\Api\models\Bill;
use app\Modules\Api\models\Push;
use app\filters\Json;
use app\Modules\Api\models\LoginForm;
use app\helpers\Request;

/**
 * Class UserController
 * @package app\Modules\Api\controllers
 */
class UserController extends ControllerBase
{
    public function actions()
    {
        return [
            'page' => [
                'class' => 'yii\web\ViewAction',
            ],
        ];
    }
    /**
     * 注册
     * @return array
     */
    public function actionRegister()
    {
        $user = new User(['scenario'=>User::SCENARIO_REGISTER]);
        if($user->load(Yii::$app->request->post(),'')&&$user->save()){
            Bill::log('新用户注册',5,$user->userid);
            return Json::success();
        }
        return Json::error(current($user->getFirstErrors()));
    }


    /**
     * 登录
     * @return array
     */
    public function actionLogin()
    {
        $loginForm = new LoginForm();
        if($loginForm->load(Yii::$app->request->post(),'')&&$loginForm->login()){
            $user = $loginForm->getUser();
            $user->phone_type = $loginForm->phone_type;
            $user->save(false);
            return Json::success(['user'=>$user]);
        }
        return Json::error(current($loginForm->getFirstErrors()));
    }

    /**
     * 检测金币
     * @return array
     */
    public function actionMoney()
    {
        $user_id = Yii::$app->request->post('user_id',0);
        $user = User::findOne($user_id);
        if(!$user) {
            $data = [
                'take_money' => '0',
                'give_money' => '0',
            ];
        }else{
            $data = $user->toArray(['take_money','give_money']);
        }
        $data['push_count'] = Push::getCount($user_id);
        return Json::success($data);
    }
    /**
     * 修改
     */
    public function actionUpdate()
    {
        $user_id = Request::strictpost('user_id','缺少用户id');
        $user = User::findOne($user_id);
        if(!$user)
            return Json::error("用户不存在");
        $user->setScenario(User::SCENARIO_PHONE_CHANGE);
        $user->name = Request::lowerpost('name',$user->name);
        $user->img = Request::lowerpost('img',$user->img);
        if(strpos($user->img,'http')){//获取的完整路径
            $imgArr = explode('/',$user->img);
            $user->img = end($imgArr);
        }
        $user->tele = Request::lowerpost('tele',$user->tele);
        if($user->save()){
            return Json::success($user);
        }
        return Json::error(current($user->getErrors()));

    }

    /**
     * 修改密码
     * @return array
     */
    public function actionChangepwd()
    {
        $user_id = Request::strictpost('user_id','缺少用户id');
        $user = User::findOne($user_id);
        if(!$user)
            return Json::error("用户不存在");
        $oldpwd = Request::strictpost('oldpwd','原密码不能为空');
        if($oldpwd != $user->pwd)
            return Json::error('原密码错误');
        $pwd = Request::strictpost('pwd','新密码不能为空');
        $user->pwd = $pwd;
        $user->save(false);
        return Json::success($user);
    }

    /**
     * 找回密码
     * @return array
     */
    public function actionFindpwd()
    {
        $tele = Request::strictpost('tele','电话不能为空');
        $user = User::find()->where(['tele'=>$tele])->one();
        if(!$user)
            return Json::error("用户不存在");
        $pwd = Request::strictpost("pwd","新密码不能为空");
        $user->pwd = $pwd;
        $user->save(false);
        return Json::success($user);
    }
    /**
     * 发送找回密码短信
     * @return array
     */
    public function actionFindpwdline()
    {
        $tele = Request::strictpost('tele','电话不能为空');
        $user = User::find()->where(['tele'=>$tele])->one();
        if(!$user)
            return Json::error("用户不存在");

        $register = rand(100000,999999);
        \app\helpers\Line::send($tele,"【竞拍】欢迎您使用竞拍,您的短信验证码是:$register,感谢您的支持！(请在五分钟内完成修改)");
        return Json::success(strval($register));
    }
    /**
     * 发送注册短信
     * @return array
     */
    public function actionSendline()
    {
        $tele = Request::strictpost('tele','缺少电话');
        $user = User::find()->where(['tele'=>$tele])->one();
        if($user)
            return Json::error("该手机号已被注册");
        $register = rand(100000,999999);
        \app\helpers\Line::send($tele,"【竞拍】欢迎您注册竞拍,您的短信验证码是:$register,感谢您的支持！(请在五分钟内完成注册)");
        return Json::success(['register'=>strval($register)]);
    }
}